<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class NoticeList extends BaseModel
{
    //定义表名
    protected $name = 'massage_service_notice_list';


    /**
     * @author chenniang
     * @DataTime: 2021-07-16 10:44
     * @功能说明:
     */
    public function getCreateTimeAttr($value,$data){

        if(!empty($value)){

            return date('Y-m-d H:i:s',$value);

        }

    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($uniacid,$order_id,$type=1,$admin_id=0){

        $data['uniacid']  = $uniacid;

        $data['order_id'] = $order_id;

        $data['type']     = $type;

        $data['admin_id'] = $admin_id;

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2023-04-20 16:09
     * @功能说明:手机端订单操作页面的通知
     */
    public function indexOrderNotice($data,$uniacid,$admin_id,$node=[]){
        //通知
        $arr = [1=>'order_id',2=>'refund_id',3=>'refuse_id'];

        $dis = [

            'uniacid' => $uniacid,

            'have_look'=> 0
        ];

        if(!empty($admin_id)&&!is_array($admin_id)){

            $dis['admin_id'] = $admin_id;

        }

        $where = [];

        if(is_array($admin_id)){

            $where[] = ['admin_id','in',$admin_id];

        }

        foreach ($arr as $k=>$value){

            $dis['type'] = $k;

            $wheres = [];

            if($k==3&&!empty($node)&&!in_array('shopRefuseOrder',$node)){

                $where[] = ['id','=',-1];

            }
            //判断权限
            if($k==1&&!empty($node)){

                $order_arr = [];

                $order_model = new Order();

                if(in_array('shopOrder',$node)){

                    $order_id = $order_model->where(['is_add'=>0])->column('id');

                    $order_arr = array_merge($order_id,$order_arr);

                }

                if(in_array('shopBellOrder',$node)){

                    $order_id = $order_model->where(['is_add'=>1])->column('id');

                    $order_arr = array_merge($order_id,$order_arr);

                }

                $where[] = ['order_id','in',$order_arr];

            }
            //判断权限
            if($k==2&&!empty($node)){

                $order_arr = [];

                $order_model = new RefundOrder();

                if(in_array('shopOrder',$node)){

                    $order_id = $order_model->where(['is_add'=>0])->column('id');

                    $order_arr = array_merge($order_id,$order_arr);

                }

                if(in_array('shopBellOrder',$node)){

                    $order_id = $order_model->where(['is_add'=>1])->column('id');

                    $order_arr = array_merge($order_id,$order_arr);

                }

                $where[] = ['order_id','in',$order_arr];

            }

            $data['notice'][$value] = $this->where($dis)->where($where)->where($wheres)->order('id desc')->field('id,order_id,type')->find();

            $data['notice'][$value] = !empty($data['notice'][$value])?$data['notice'][$value]:[];
        }

        return $data;

    }







}